using System;
using System.Collections.Generic;
using SimpleJSON;
using UnityEngine;

namespace Gj.DarkLoong
{
    public class RedemptionCode
    {
        public static void Valid(string code, Action<JSONArray, string> callback)
        {
            Debug.Log($"valid code: {code} {Auth.IsLogin()}");
            if (!Auth.IsLogin()) return;
            Project.ProjectUpdate.Queue.Enqueue(async () =>
            {
                Debug.Log($"valid code: {code}");
                var data = new JSONObject();
                data["code"] = code;
                var header = Api.CookieHeader();
                header["token"] = Auth.SessionToken;
                var response = await Api.Post($"/workshop/redemption_code/{code}/valid", data, header);

                var err = response["status"].AsInt != 200 ? response["message"].Value : "";
                var result = response["result"];

                callback(result.AsArray, err);
            });

        }
    }
}